const path = require('path');
const multer = require('koa-multer');

function getDirNameByDate() {
  const date = new Date();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  return `${date.getFullYear()}${month}${date.getDate()}`;
}

const storage = multer.diskStorage({
  destination: path.join('./uploads/', getDirNameByDate()),
  filename(req, file, cb) {
    const type = file.originalname.split('.')[1]
    cb(null, `${Math.random().toString(16).slice(8)}${Date.now().toString(16)}.${type}`)
  },
});

const upload = multer({ storage });

const handleUpload = upload.array('pictures');

module.exports = {
  handleUpload,
};
